home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gdevdflt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-30  |  26.8 KB  |  905 lines

  1. /* Copyright (C) 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevdflt.c */
  20. /* Default device implementation */
  21. #include "gx.h"
  22. #include "gpcheck.h"
  23. #include "gserrors.h"
  24. #include "gsbittab.h"
  25. #include "gsropt.h"
  26. #include "gxdcolor.h"
  27. #include "gxdevice.h"
  28. #include "gxdevmem.h"
  29. #include "gxcpath.h"
  30.  
  31. /* ---------------- Default device procedures ---------------- */
  32.  
  33. /* Define the default implementations of RasterOp procedures. */
  34. /* If the RasterOp option is linked in, it initializes these */
  35. /* to different values. */
  36. dev_proc_copy_rop((*gx_default_copy_rop_proc)) = gx_no_copy_rop;
  37. dev_proc_copy_rop((*gx_forward_copy_rop_proc)) = gx_no_copy_rop;
  38. dev_proc_strip_copy_rop((*gx_default_strip_copy_rop_proc)) = gx_no_strip_copy_rop;
  39. dev_proc_strip_copy_rop((*gx_forward_strip_copy_rop_proc)) = gx_no_strip_copy_rop;
  40.  
  41. /* Fill in NULL procedures in a device procedure record. */
  42. void
  43. gx_device_fill_in_procs(register gx_device *dev)
  44. {    gx_device_set_procs(dev);
  45.     fill_dev_proc(dev, open_device, gx_default_open_device);
  46.     fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
  47.     fill_dev_proc(dev, sync_output, gx_default_sync_output);
  48.     fill_dev_proc(dev, output_page, gx_default_output_page);
  49.     fill_dev_proc(dev, close_device, gx_default_close_device);
  50.     fill_dev_proc(dev, map_rgb_color, gx_default_map_rgb_color);
  51.     fill_dev_proc(dev, map_color_rgb, gx_default_map_color_rgb);
  52.     /* NOT fill_rectangle */
  53.     fill_dev_proc(dev, tile_rectangle, gx_default_tile_rectangle);
  54.     fill_dev_proc(dev, copy_mono, gx_default_copy_mono);
  55.     fill_dev_proc(dev, copy_color, gx_default_copy_color);
  56.     fill_dev_proc(dev, draw_line, gx_default_draw_line);
  57.     fill_dev_proc(dev, get_bits, gx_default_get_bits);
  58.     fill_dev_proc(dev, get_params, gx_default_get_params);
  59.     fill_dev_proc(dev, put_params, gx_default_put_params);
  60.     fill_dev_proc(dev, map_cmyk_color, gx_default_map_cmyk_color);
  61.     fill_dev_proc(dev, get_xfont_procs, gx_default_get_xfont_procs);
  62.     fill_dev_proc(dev, get_xfont_device, gx_default_get_xfont_device);
  63.     fill_dev_proc(dev, map_rgb_alpha_color, gx_default_map_rgb_alpha_color);
  64.     fill_dev_proc(dev, get_page_device, gx_default_get_page_device);
  65.     fill_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
  66.     fill_dev_proc(dev, copy_alpha, gx_default_copy_alpha);
  67.     fill_dev_proc(dev, get_band, gx_default_get_band);
  68.     fill_dev_proc(dev, copy_rop, gx_default_copy_rop_proc);
  69.     fill_dev_proc(dev, fill_path, gx_default_fill_path);
  70.     fill_dev_proc(dev, stroke_path, gx_default_stroke_path);
  71.     fill_dev_proc(dev, fill_mask, gx_default_fill_mask);
  72.     fill_dev_proc(dev, fill_trapezoid, gx_default_fill_trapezoid);
  73.     fill_dev_proc(dev, fill_parallelogram, gx_default_fill_parallelogram);
  74.     fill_dev_proc(dev, fill_triangle, gx_default_fill_triangle);
  75.     fill_dev_proc(dev, draw_thin_line, gx_default_draw_thin_line);
  76.     fill_dev_proc(dev, begin_image, gx_default_begin_image);
  77.     fill_dev_proc(dev, image_data, gx_default_image_data);
  78.     fill_dev_proc(dev, end_image, gx_default_end_image);
  79.     fill_dev_proc(dev, strip_tile_rectangle, gx_default_strip_tile_rectangle);
  80.     fill_dev_proc(dev, strip_copy_rop, gx_default_strip_copy_rop_proc);
  81.     fill_dev_proc(dev, get_clipping_box, gx_default_get_clipping_box);
  82. }
  83.  
  84. int
  85. gx_default_open_device(gx_device *dev)
  86. {    return 0;
  87. }
  88.  
  89. /* Get the initial matrix for a device with inverted Y. */
  90. /* This includes essentially all printers and displays. */
  91. void
  92. gx_default_get_initial_matrix(gx_device *dev, register gs_matrix *pmat)
  93. {    pmat->xx = dev->HWResolution[0] / 72.0;  /* x_pixels_per_inch */
  94.     pmat->xy = 0;
  95.     pmat->yx = 0;
  96.     pmat->yy = dev->HWResolution[1] / -72.0;  /* y_pixels_per_inch */
  97.         /****** tx/y is WRONG for devices with ******/
  98.         /****** arbitrary initial matrix ******/
  99.     pmat->tx = 0;
  100.     pmat->ty = dev->height;
  101. }
  102. /* Get the initial matrix for a device with upright Y. */
  103. /* This includes just a few printers and window systems. */
  104. void
  105. gx_upright_get_initial_matrix(gx_device *dev, register gs_matrix *pmat)
  106. {    pmat->xx = dev->HWResolution[0] / 72.0;  /* x_pixels_per_inch */
  107.     pmat->xy = 0;
  108.     pmat->yx = 0;
  109.     pmat->yy = dev->HWResolution[1] / 72.0;  /* y_pixels_per_inch */
  110.         /****** tx/y is WRONG for devices with ******/
  111.         /****** arbitrary initial matrix ******/
  112.     pmat->tx = 0;
  113.     pmat->ty = 0;
  114. }
  115.  
  116. int
  117. gx_default_sync_output(gx_device *dev)
  118. {    return 0;
  119. }
  120.  
  121. int
  122. gx_default_output_page(gx_device *dev, int num_copies, int flush)
  123. {    return (*dev_proc(dev, sync_output))(dev);
  124. }
  125.  
  126. int
  127. gx_default_close_device(gx_device *dev)
  128. {    return 0;
  129. }
  130.  
  131. /* By default, implement tile_rectangle using strip_tile_rectangle. */
  132. int
  133. gx_default_tile_rectangle(gx_device *dev, const gx_tile_bitmap *tile,
  134.   int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
  135.   int px, int py)
  136. {    gx_strip_bitmap tiles;
  137.     *(gx_tile_bitmap *)&tiles = *tile;
  138.     tiles.shift = tiles.rep_shift = 0;
  139.     return (*dev_proc(dev, strip_tile_rectangle))
  140.       (dev, &tiles, x, y, w, h, color0, color1, px, py);
  141. }
  142.  
  143. /* Implement copy_mono by filling lots of small rectangles. */
  144. /* This is very inefficient, but it works as a default. */
  145. int
  146. gx_default_copy_mono(gx_device *dev, const byte *data,
  147.   int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  148.   gx_color_index zero, gx_color_index one)
  149. {    bool invert;
  150.     gx_color_index color;
  151.     gx_device_color devc;
  152.  
  153.     fit_copy(dev, data, dx, raster, id, x, y, w, h);
  154.     if ( one != gx_no_color_index )
  155.       { invert = false;
  156.         color = one;
  157.         if ( zero != gx_no_color_index )
  158.           { int code = (*dev_proc(dev, fill_rectangle))
  159.           (dev, x, y, w, h, zero);
  160.         if ( code < 0 )
  161.           return code;
  162.           }
  163.       }
  164.     else
  165.       { invert = true;
  166.         color = zero;
  167.       }
  168.     color_set_pure(&devc, color);
  169.     return gx_dc_default_fill_masked
  170.       (&devc, data, dx, raster, id, x, y, w, h, dev, rop3_T, invert);
  171. }
  172.  
  173. /* Implement copy_color by filling lots of small rectangles. */
  174. /* This is very inefficient, but it works as a default. */
  175. int
  176. gx_default_copy_color(gx_device *dev, const byte *data,
  177.   int dx, int raster, gx_bitmap_id id,
  178.   int x, int y, int w, int h)
  179. {    int depth = dev->color_info.depth;
  180.     byte mask;
  181.     dev_proc_fill_rectangle((*fill));
  182.     const byte *row;
  183.     int iy;
  184.  
  185.     if ( depth == 1 )
  186.       return (*dev_proc(dev, copy_mono))(dev, data, dx, raster, id,
  187.                 x, y, w, h,
  188.                 (gx_color_index)0, (gx_color_index)1);
  189.     fit_copy(dev, data, dx, raster, id, x, y, w, h);
  190.     fill = dev_proc(dev, fill_rectangle);
  191.     mask = (byte)((1 << depth) - 1);
  192.     for ( row = data, iy = 0; iy < h; row += raster, ++iy )
  193.       { int ix;
  194.         gx_color_index c0 = gx_no_color_index;
  195.         const byte *ptr = row + ((dx * depth) >> 3);
  196.         int i0;
  197.  
  198.         for ( i0 = ix = 0; ix < w; ++ix )
  199.           { gx_color_index color;
  200.  
  201.         if ( depth >= 8 )
  202.           { color = *ptr++;
  203.             switch ( depth )
  204.               {
  205.               case 32: color = (color << 8) + *ptr++;
  206.               case 24: color = (color << 8) + *ptr++;
  207.               case 16: color = (color << 8) + *ptr++;
  208.               }
  209.           }
  210.         else
  211.           { uint dbit = (-(ix + dx + 1) * depth) & 7;
  212.             color = (*ptr >> dbit) & mask;
  213.             if ( dbit == 0 )
  214.               ptr++;
  215.           }
  216.         if ( color != c0 )
  217.           { if ( ix > i0 )
  218.               { int code = (*fill)
  219.               (dev, i0 + x, iy + y, ix - i0, 1, c0);
  220.             if ( code < 0 )
  221.               return code;
  222.               }
  223.             c0 = color;
  224.             i0 = ix;
  225.           }
  226.           }
  227.         if ( ix > i0 )
  228.           { int code = (*fill)(dev, i0 + x, iy + y, ix - i0, 1, c0);
  229.         if ( code < 0 )
  230.           return code;
  231.           }
  232.       }
  233.     return 0;
  234. }
  235.  
  236. int
  237. gx_default_get_bits(gx_device *dev, int y, byte *data, byte **actual_data)
  238. {    return_error(gs_error_unknownerror);
  239. }
  240.  
  241. gx_xfont_procs *
  242. gx_default_get_xfont_procs(gx_device *dev)
  243. {    return NULL;
  244. }
  245.  
  246. gx_device *
  247. gx_default_get_xfont_device(gx_device *dev)
  248. {    return dev;
  249. }
  250.  
  251. gx_device *
  252. gx_default_get_page_device(gx_device *dev)
  253. {    return NULL;
  254. }
  255. gx_device *
  256. gx_page_device_get_page_device(gx_device *dev)
  257. {    return dev;
  258. }
  259.  
  260. int
  261. gx_default_get_alpha_bits(gx_device *dev, graphics_object_type type)
  262. {    return 1;
  263. }
  264.  
  265. int
  266. gx_no_copy_alpha(gx_device *dev, const byte *data, int data_x,
  267.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  268.   gx_color_index color, int depth)
  269. {    return_error(gs_error_unknownerror);
  270. }
  271.  
  272. int
  273. gx_default_copy_alpha(gx_device *dev, const byte *data, int data_x,
  274.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  275.   gx_color_index color, int depth)
  276. {    /* This might be called with depth = 1.... */
  277.     if ( depth == 1 )
  278.       return (*dev_proc(dev, copy_mono))(dev, data, data_x, raster, id,
  279.                          x, y, width, height,
  280.                          gx_no_color_index, color);
  281.     /*
  282.      * Simulate alpha by weighted averaging of RGB values.
  283.      * This is very slow, but functionally correct.
  284.      */
  285.     { const byte *row;
  286.       gs_memory_t *mem = &gs_memory_default; /* dev might not have one */
  287.       int bpp = dev->color_info.depth;
  288.       uint in_size = gx_device_raster(dev, false);
  289.       byte *lin;
  290.       uint out_size;
  291.       byte *lout;
  292.       int code = 0;
  293.       gx_color_value color_rgb[3];
  294.       int ry;
  295.  
  296.       fit_copy(dev, data, data_x, raster, id, x, y, width, height);
  297.       row = data;
  298.       out_size = bitmap_raster(width * bpp);
  299.       lin = gs_alloc_bytes(mem, in_size, "copy_alpha(lin)");
  300.       lout = gs_alloc_bytes(mem, out_size, "copy_alpha(lout)");
  301.       if ( lin == 0 || lout == 0 )
  302.         { code = gs_note_error(gs_error_VMerror);
  303.           goto out;
  304.         }
  305.       (*dev_proc(dev, map_color_rgb))(dev, color, color_rgb);
  306.       for ( ry = y; ry < y + height; row += raster, ++ry )
  307.         { byte *line;
  308.           int sx, rx;
  309.           declare_line_accum(lout, bpp, x);
  310.  
  311.           code = (*dev_proc(dev, get_bits))(dev, ry, lin, &line);
  312.           if ( code < 0 )
  313.         break;
  314.           for ( sx = data_x, rx = x; sx < data_x + width; ++sx, ++rx )
  315.         { gx_color_index previous = gx_no_color_index;
  316.           gx_color_index composite;
  317.           int alpha2, alpha;
  318.  
  319.           if ( depth == 2 )    /* map 0 - 3 to 0 - 15 */
  320.             alpha = ((row[sx >> 2] >> ((3 - (sx & 3)) << 1)) & 3) * 5;
  321.           else 
  322.             alpha2 = row[sx >> 1],
  323.             alpha = (sx & 1 ? alpha2 & 0xf : alpha2 >> 4);
  324. blend:          if ( alpha == 15 )
  325.             { /* Just write the new color. */
  326.               composite = color;
  327.             }
  328.           else
  329.             { if ( previous == gx_no_color_index )
  330.             { /* Extract the old color. */
  331.               if ( bpp < 8 )
  332.                 { const uint bit = rx * bpp;
  333.                   const byte *src = line + (bit >> 3);
  334.                   previous =
  335.                 (*src >> (8 - (bit + bpp))) &
  336.                   ((1 << bpp) - 1);
  337.                 }
  338.               else
  339.                 { const byte *src = line + (rx * (bpp >> 3));
  340.                   previous = 0;
  341.                   switch ( bpp >> 3 )
  342.                 {
  343.                 case 4:
  344.                   previous += (gx_color_index)*src++ << 24;
  345.                 case 3:
  346.                   previous += (gx_color_index)*src++ << 16;
  347.                 case 2:
  348.                   previous += (gx_color_index)*src++ << 8;
  349.                 case 1:
  350.                   previous += *src++;
  351.                 }
  352.                 }
  353.             }
  354.               if ( alpha == 0 )
  355.             { /* Just write the old color. */
  356.               composite = previous;
  357.             }
  358.               else
  359.             { /* Blend RGB values. */
  360.               gx_color_value rgb[3];
  361.  
  362.               (*dev_proc(dev, map_color_rgb))(dev, previous, rgb);
  363. #if arch_ints_are_short
  364. #  define b_int long
  365. #else
  366. #  define b_int int
  367. #endif
  368. #define make_shade(old, clr, alpha, amax) \
  369.   (old) + (((b_int)(clr) - (b_int)(old)) * (alpha) / (amax))
  370.               rgb[0] = make_shade(rgb[0], color_rgb[0], alpha, 15);
  371.               rgb[1] = make_shade(rgb[1], color_rgb[1], alpha, 15);
  372.               rgb[2] = make_shade(rgb[2], color_rgb[2], alpha, 15);
  373. #undef b_int
  374. #undef make_shade
  375.               composite =
  376.                 (*dev_proc(dev, map_rgb_color))(dev, rgb[0],
  377.                                 rgb[1], rgb[2]);
  378.               if ( composite == gx_no_color_index )
  379.                 { /* The device can't represent this color. */
  380.                   /* Move the alpha value towards 0 or 1. */
  381.                   if ( alpha == 7 )    /* move 1/2 towards 1 */
  382.                 ++alpha;
  383.                   alpha = (alpha & 8) | (alpha >> 1);
  384.                   goto blend;
  385.                 }
  386.             }
  387.             }
  388.           line_accum(composite, bpp);
  389.         }
  390.           line_accum_copy(dev, lout, bpp, x, rx, raster, ry);
  391.         }
  392. out:      gs_free_object(mem, lout, "copy_alpha(lout)");
  393.       gs_free_object(mem, lin, "copy_alpha(lin)");
  394.       return code;
  395.     }
  396. }
  397.  
  398. int
  399. gx_default_get_band(gx_device *dev, int y, int *band_start)
  400. {    return 0;
  401. }
  402.  
  403. int
  404. gx_no_copy_rop(gx_device *dev,
  405.   const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  406.   const gx_color_index *scolors,
  407.   const gx_tile_bitmap *texture, const gx_color_index *tcolors,
  408.   int x, int y, int width, int height,
  409.   int phase_x, int phase_y, gs_logical_operation_t lop)
  410. {    return_error(gs_error_unknownerror);    /* not implemented */
  411. }
  412. int
  413. gx_default_copy_rop(gx_device *dev,
  414.   const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  415.   const gx_color_index *scolors,
  416.   const gx_tile_bitmap *texture, const gx_color_index *tcolors,
  417.   int x, int y, int width, int height,
  418.   int phase_x, int phase_y, gs_logical_operation_t lop)
  419. {    return (*gx_default_copy_rop_proc)
  420.       (dev, sdata, sourcex, sraster, id, scolors, texture, tcolors,
  421.        x, y, width, height, phase_x, phase_y, lop);
  422. }
  423.  
  424. int
  425. gx_default_fill_mask(gx_device *orig_dev,
  426.   const byte *data, int dx, int raster, gx_bitmap_id id,
  427.   int x, int y, int w, int h,
  428.   const gx_drawing_color *pdcolor, int depth,
  429.   gs_logical_operation_t lop, const gx_clip_path *pcpath)
  430. {    gx_device *dev;
  431.     gx_device_clip cdev;
  432.     gx_color_index colors[2];
  433.     gx_strip_bitmap *tile;
  434.  
  435.     if ( gx_dc_is_pure(pdcolor) )
  436.       { tile = 0;
  437.         colors[0] = gx_no_color_index;
  438.         colors[1] = gx_dc_pure_color(pdcolor);
  439.       }
  440.     else if ( gx_dc_is_binary_halftone(pdcolor) )
  441.       { tile = gx_dc_binary_tile(pdcolor);
  442.         colors[0] = gx_dc_binary_color0(pdcolor);
  443.         colors[1] = gx_dc_binary_color1(pdcolor);
  444.       }
  445.     else
  446.       return_error(gs_error_unknownerror);    /* not implemented */
  447.     if ( pcpath != 0 )
  448.       {    gx_make_clip_path_device(&cdev, pcpath);
  449.         cdev.target = orig_dev;
  450.         dev = (gx_device *)&cdev;
  451.         (*dev_proc(dev, open_device))(dev);
  452.       }
  453.     else
  454.       dev = orig_dev;
  455.     if ( depth > 1 )
  456.       { /****** CAN'T DO ROP OR HALFTONE WITH ALPHA ******/
  457.         return (*dev_proc(dev, copy_alpha))
  458.           (dev, data, dx, raster, id, x, y, w, h, colors[1], depth);
  459.       }
  460.     if ( lop != lop_default )
  461.       { gx_color_index scolors[2];
  462.  
  463.         scolors[0] =    /* white */
  464.           (*dev_proc(dev, map_rgb_color))
  465.             (dev, gx_max_color_value, gx_max_color_value, gx_max_color_value);
  466.         scolors[1] =    /* black */
  467.           (*dev_proc(dev, map_rgb_color))
  468.             (dev, (gx_color_value)0, (gx_color_value)0, (gx_color_value)0);
  469.         if ( tile == 0 )
  470.           colors[0] = colors[1];    /* pure color */
  471.         /* We want to write only where the mask is a 1, */
  472.         /* so enable source transparency. */
  473.         return (*dev_proc(dev, strip_copy_rop))
  474.           (dev, data, dx, raster, id, scolors, tile, colors,
  475.            x, y, w, h,
  476.            gx_dc_phase(pdcolor).x, gx_dc_phase(pdcolor).y,
  477.            lop | lop_S_transparent);
  478.       }
  479.     if ( tile == 0 )
  480.       { return (*dev_proc(dev, copy_mono))
  481.           (dev, data, dx, raster, id, x, y, w, h,
  482.            gx_no_color_index, colors[1]);
  483.       }
  484.     /*
  485.      * Use the same approach as the default copy_mono (above).  We
  486.      * should really clip to the intersection of the bounding boxes of
  487.      * the device and the clipping path, but it's too much work.
  488.      */
  489.     fit_copy(orig_dev, data, dx, raster, id, x, y, w, h);
  490.     { dev_proc_strip_tile_rectangle((*tile_proc)) =
  491.         dev_proc(dev, strip_tile_rectangle);
  492.       const byte *row = data + (dx >> 3);
  493.       int dx_bit = dx & 7;
  494.       int wdx = w + dx_bit;
  495.       int iy;
  496.  
  497.       for ( row = data, iy = 0; iy < h; row += raster, iy++ )
  498.         { int ix;
  499.           for ( ix = dx_bit; ix < wdx; )
  500.         { int i0;
  501.           uint b;
  502.           uint len;
  503.           int code;
  504.  
  505.           /* Skip 0-bits. */
  506.           b = row[ix >> 3];
  507.           len = byte_bit_run_length[ix & 7][b ^ 0xff];
  508.           if ( len )
  509.             { ix += ((len - 1) & 7) + 1;
  510.               continue;
  511.             }
  512.           /* Scan 1-bits. */
  513.           i0 = ix;
  514.           for ( ; ; )
  515.             { b = row[ix >> 3];
  516.               len = byte_bit_run_length[ix & 7][b];
  517.               if ( !len )
  518.             break;
  519.               ix += ((len - 1) & 7) + 1;
  520.               if ( ix >= wdx )
  521.             { ix = wdx;
  522.               break;
  523.             }
  524.               if ( len < 8 )
  525.             break;
  526.             }
  527.           /* Now color the run from i0 to ix. */
  528.           code = (*tile_proc)
  529.             (dev, tile, i0 - dx_bit + x, iy + y, ix - i0, 1,
  530.              colors[0], colors[1],
  531.              gx_dc_phase(pdcolor).x, gx_dc_phase(pdcolor).y);
  532.           if ( code < 0 )
  533.             return code;
  534. #undef row_bit
  535.         }
  536.         }
  537.     }
  538.     return 0;
  539. }
  540.  
  541. /* Default implementation of strip_tile_rectangle */
  542. int
  543. gx_default_strip_tile_rectangle(gx_device *dev, const gx_strip_bitmap *tiles,
  544.   int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
  545.   int px, int py)
  546. {    /* Fill the rectangle in chunks. */
  547.     int width = tiles->size.x;
  548.     int height = tiles->size.y;
  549.     int raster = tiles->raster;
  550.     int rwidth = tiles->rep_width;
  551.     int rheight = tiles->rep_height;
  552.     int shift = tiles->shift;
  553.     int xoff =
  554.       (shift == 0 ? px :
  555.        px + (y + py) / rheight * tiles->rep_shift);
  556.     int irx = ((rwidth & (rwidth - 1)) == 0 ?    /* power of 2 */
  557.            (x + xoff) & (rwidth - 1) :
  558.            (x + xoff) % rwidth);
  559.     int ry = ((rheight & (rheight - 1)) == 0 ?    /* power of 2 */
  560.           (y + py) & (rheight - 1) :
  561.           (y + py) % rheight);
  562.     int icw = width - irx;
  563.     int ch = height - ry;
  564.     byte *row = tiles->data + ry * raster;
  565.     dev_proc_copy_mono((*proc_mono));
  566.     dev_proc_copy_color((*proc_color));
  567.     int code;
  568.  
  569. #ifdef DEBUG
  570. if ( gs_debug_c('t') )
  571.    {    int ptx, pty;
  572.     const byte *ptp = tiles->data;
  573.     dprintf3("[t]tile %dx%d raster=%d;",
  574.         tiles->size.x, tiles->size.y, tiles->raster);
  575.     dprintf6(" x,y=%d,%d w,h=%d,%d p=%d,%d\n",
  576.         x, y, w, h, px, py);
  577.     for ( pty = 0; pty < tiles->size.y; pty++ )
  578.        {    dprintf("   ");
  579.         for ( ptx = 0; ptx < tiles->raster; ptx++ )
  580.             dprintf1("%3x", *ptp++);
  581.        }
  582.     dputc('\n');
  583.    }
  584. #endif
  585.  
  586.     /*
  587.      * We should really make the following check before doing
  588.      * all the computations above, but since we expect devices
  589.      * to implement strip_tile_rectangle rather than tile_rectangle,
  590.      * the check will rarely succeed.
  591.      */
  592.     if ( dev_proc(dev, tile_rectangle) != gx_default_tile_rectangle )
  593.       { if ( shift == 0 )
  594.           { /*
  595.          * Temporarily patch the tile_rectangle procedure in the
  596.          * device so we don't get into a recursion loop if the
  597.          * device has a tile_rectangle procedure that conditionally
  598.          * calls the strip_tile_rectangle procedure.
  599.          */
  600.         dev_proc_tile_rectangle((*tile_proc)) =
  601.           dev_proc(dev, tile_rectangle);
  602.         int code;
  603.  
  604.         set_dev_proc(dev, tile_rectangle, gx_default_tile_rectangle);
  605.         code = (*tile_proc)
  606.           (dev, (const gx_tile_bitmap *)tiles, x, y, w, h,
  607.            color0, color1, px, py);
  608.         set_dev_proc(dev, tile_rectangle, tile_proc);
  609.         return code;
  610.           }
  611.         /* We should probably optimize this case too, for the benefit */
  612.         /* of window systems, but we don't yet. */
  613.       }
  614.  
  615.     if ( color0 == gx_no_color_index && color1 == gx_no_color_index )
  616.       proc_color = dev_proc(dev, copy_color);
  617.     else
  618.       proc_color = 0, proc_mono = dev_proc(dev, copy_mono);
  619.  
  620. /****** SHOULD ALSO PASS id IF COPYING A FULL TILE ******/
  621. #define real_copy_tile(srcx, tx, ty, tw, th)\
  622.   code =\
  623.     (proc_color != 0 ?\
  624.      (*proc_color)(dev, row, srcx, raster, gx_no_bitmap_id, tx, ty, tw, th) :\
  625.      (*proc_mono)(dev, row, srcx, raster, gx_no_bitmap_id, tx, ty, tw, th, color0, color1));\
  626.   if ( code < 0 ) return_error(code);\
  627.   return_if_interrupt()
  628. #ifdef DEBUG
  629. #define copy_tile(sx, tx, ty, tw, th)\
  630.   if ( gs_debug_c('t') )\
  631.     dprintf5("   copy sx=%d x=%d y=%d w=%d h=%d\n",\
  632.          sx, tx, ty, tw, th);\
  633.   real_copy_tile(sx, tx, ty, tw, th)
  634. #else
  635. #define copy_tile(sx, tx, ty, tw, th)\
  636.   real_copy_tile(sx, tx, ty, tw, th)
  637. #endif
  638.     if ( ch >= h )
  639.        {    /* Shallow operation */
  640.         if ( icw >= w )
  641.            {    /* Just one (partial) tile to transfer. */
  642.             copy_tile(irx, x, y, w, h);
  643.            }
  644.         else
  645.           {    int ex = x + w;
  646.             int fex = ex - width;
  647.             int cx = x + icw;
  648.  
  649.             copy_tile(irx, x, y, icw, h);
  650.             while ( cx <= fex )
  651.               {    copy_tile(0, cx, y, width, h);
  652.                 cx += width;
  653.                   }
  654.             if ( cx < ex )
  655.               {    copy_tile(0, cx, y, ex - cx, h);
  656.               }
  657.           }
  658.       }
  659.     else if ( icw >= w && shift == 0 )
  660.        {    /* Narrow operation, no shift */
  661.         int ey = y + h;
  662.         int fey = ey - height;
  663.         int cy = y + ch;
  664.  
  665.         copy_tile(irx, x, y, w, ch);
  666.         row = tiles->data;
  667.         do
  668.            {    ch = (cy > fey ? ey - cy : height);
  669.             copy_tile(irx, x, cy, w, ch);
  670.            }
  671.         while ( (cy += ch) < ey );
  672.        }
  673.     else
  674.        {    /* Full operation.  If shift != 0, some scan lines */
  675.         /* may be narrow.  We could test shift == 0 in advance */
  676.         /* and use a slightly faster loop, but right now */
  677.         /* we don't bother. */
  678.         int ex = x + w, ey = y + h;
  679.         int fex = ex - width, fey = ey - height;
  680.         int cx, cy;
  681.  
  682.         for ( cy = y; ; )
  683.            {    if ( icw >= w )
  684.               { copy_tile(irx, x, cy, w, ch);
  685.               }
  686.             else
  687.               { copy_tile(irx, x, cy, icw, ch);
  688.                 cx = x + icw;
  689.                 while ( cx <= fex )
  690.                   { copy_tile(0, cx, cy, width, ch);
  691.                 cx += width;
  692.                   }
  693.                 if ( cx < ex )
  694.                   { copy_tile(0, cx, cy, ex - cx, ch);
  695.                   }
  696.               }
  697.             if ( (cy += ch) >= ey ) break;
  698.             ch = (cy > fey ? ey - cy : height);
  699.             if ( (irx += shift) >= rwidth )
  700.               irx -= rwidth;
  701.             icw = width - irx;
  702.             row = tiles->data;
  703.            }
  704.        }
  705. #undef copy_tile
  706. #undef real_copy_tile
  707.     return 0;
  708. }
  709.  
  710. int
  711. gx_no_strip_copy_rop(gx_device *dev,
  712.   const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  713.   const gx_color_index *scolors,
  714.   const gx_strip_bitmap *textures, const gx_color_index *tcolors,
  715.   int x, int y, int width, int height,
  716.   int phase_x, int phase_y, gs_logical_operation_t lop)
  717. {    return_error(gs_error_unknownerror);    /* not implemented */
  718. }
  719. int
  720. gx_default_strip_copy_rop(gx_device *dev,
  721.   const byte *sdata, int sourcex, uint sraster, gx_bitmap_id id,
  722.   const gx_color_index *scolors,
  723.   const gx_strip_bitmap *textures, const gx_color_index *tcolors,
  724.   int x, int y, int width, int height,
  725.   int phase_x, int phase_y, gs_logical_operation_t lop)
  726. {    return (*gx_default_strip_copy_rop_proc)
  727.       (dev, sdata, sourcex, sraster, id, scolors, textures, tcolors,
  728.        x, y, width, height, phase_x, phase_y, lop);
  729. }
  730.  
  731. void
  732. gx_default_get_clipping_box(gx_device *dev, gs_fixed_rect *pbox)
  733. {    pbox->p.x = 0;
  734.     pbox->p.y = 0;
  735.     pbox->q.x = int2fixed(dev->width);
  736.     pbox->q.y = int2fixed(dev->height);
  737. }
  738. void
  739. gx_get_largest_clipping_box(gx_device *dev, gs_fixed_rect *pbox)
  740. {    pbox->p.x = min_fixed;
  741.     pbox->p.y = min_fixed;
  742.     pbox->q.x = max_fixed;
  743.     pbox->q.y = max_fixed;
  744. }
  745.  
  746. /* The following is not really a device procedure.  See gxdevice.h. */
  747.  
  748. /* Create an ordinary memory device for page or band buffering. */
  749. int
  750. gx_default_make_buffer_device(gx_device_memory *mdev,
  751.   gx_device *target, gs_memory_t *mem, bool for_band)
  752. {    const gx_device_memory *mdproto =
  753.       gdev_mem_device_for_bits(target->color_info.depth);
  754.  
  755.     if ( mdproto == 0 )
  756.       return_error(gs_error_rangecheck);
  757.     if ( target == (gx_device *)mdev )
  758.       mdev->std_procs = mdproto->std_procs;
  759.     else
  760.       gs_make_mem_device(mdev, mdproto, mem, (for_band ? 1 : 0),
  761.                  (target == (gx_device *)mdev ? 0 : target));
  762.     return 0;
  763. }
  764.  
  765. /* ---------------- Default per-instance procedures ---------------- */
  766.  
  767. int
  768. gx_default_install(gx_device *dev, gs_state *pgs)
  769. {    return 0;
  770. }
  771.  
  772. int
  773. gx_default_begin_page(gx_device *dev, gs_state *pgs)
  774. {    return 0;
  775. }
  776.  
  777. int
  778. gx_default_end_page(gx_device *dev, int reason, gs_state *pgs)
  779. {    return (reason != 2 ? 1 : 0);
  780. }
  781.  
  782. /* ---------------- Unaligned copy operations ---------------- */
  783.  
  784. /*
  785.  * Implementing unaligned operations in terms of the standard aligned
  786.  * operations requires adjusting the bitmap origin and/or the raster to be
  787.  * aligned.  Adjusting the origin is simple; adjusting the raster requires
  788.  * doing the operation one scan line at a time.
  789.  */
  790. int
  791. gx_copy_mono_unaligned(gx_device *dev, const byte *data,
  792.   int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  793.   gx_color_index zero, gx_color_index one)
  794. {    dev_proc_copy_mono((*copy_mono)) = dev_proc(dev, copy_mono);
  795.     uint offset = alignment_mod(data, align_bitmap_mod);
  796.     int step = raster & (align_bitmap_mod - 1);
  797.  
  798.     /* Adjust the origin. */
  799.     data -= offset;
  800.     dx += offset << 3;
  801.  
  802.     /* Adjust the raster. */
  803.     if ( !step )
  804.       { /* No adjustment needed. */
  805.         return (*copy_mono)(dev, data, dx, raster, id,
  806.                 x, y, w, h, zero, one);
  807.       }
  808.  
  809.     /* Do the transfer one scan line at a time. */
  810.     { const byte *p = data;
  811.       int d = dx;
  812.       int code = 0;
  813.       int i;
  814.  
  815.       for ( i = 0; i < h && code >= 0;
  816.             ++i, p += raster - step, d += step << 3
  817.           )
  818.         code = (*copy_mono)(dev, p, d, raster, gx_no_bitmap_id,
  819.                 x, y + i, w, 1, zero, one);
  820.       return code;
  821.     }
  822. }
  823.  
  824. int
  825. gx_copy_color_unaligned(gx_device *dev, const byte *data,
  826.   int data_x, int raster, gx_bitmap_id id,
  827.   int x, int y, int width, int height)
  828. {    dev_proc_copy_color((*copy_color)) = dev_proc(dev, copy_color);
  829.     int depth = dev->color_info.depth;
  830.     uint offset = (uint)(data - (const byte *)0) & (align_bitmap_mod - 1);
  831.     int step = raster & (align_bitmap_mod - 1);
  832.  
  833.     /*
  834.      * Adjust the origin.
  835.      * We have to do something very special for 24-bit data,
  836.      * because that is the only depth that doesn't divide
  837.      * align_bitmap_mod exactly.  In particular, we need to find
  838.      * M*B + R == 0 mod 3, where M is align_bitmap_mod, R is the
  839.      * offset value just calculated, and B is an integer unknown;
  840.      * the new value of offset will be M*B + R.
  841.      */
  842.     if ( depth == 24 )
  843.       offset += (offset % 3) *
  844.         (align_bitmap_mod * (3 - (align_bitmap_mod % 3)));
  845.     data -= offset;
  846.     data_x += (offset << 3) / depth;
  847.  
  848.     /* Adjust the raster. */
  849.     if ( !step )
  850.       { /* No adjustment needed. */
  851.         return (*copy_color)(dev, data, data_x, raster, id,
  852.                  x, y, width, height);
  853.       }
  854.  
  855.     /* Do the transfer one scan line at a time. */
  856.     { const byte *p = data;
  857.       int d = data_x;
  858.       int dstep = (step << 3) / depth;
  859.       int code = 0;
  860.       int i;
  861.  
  862.       for ( i = 0; i < height && code >= 0;
  863.             ++i, p += raster - step, d += dstep
  864.           )
  865.         code = (*copy_color)(dev, p, d, raster, gx_no_bitmap_id,
  866.                  x, y + i, width, 1);
  867.       return code;
  868.     }
  869. }
  870.  
  871. int
  872. gx_copy_alpha_unaligned(gx_device *dev, const byte *data, int data_x,
  873.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  874.   gx_color_index color, int depth)
  875. {    dev_proc_copy_alpha((*copy_alpha)) = dev_proc(dev, copy_alpha);
  876.     uint offset = (uint)(data - (const byte *)0) & (align_bitmap_mod - 1);
  877.     int step = raster & (align_bitmap_mod - 1);
  878.  
  879.     /* Adjust the origin. */
  880.     data -= offset;
  881.     data_x += (offset << 3) / depth;
  882.  
  883.     /* Adjust the raster. */
  884.     if ( !step )
  885.       { /* No adjustment needed. */
  886.         return (*copy_alpha)(dev, data, data_x, raster, id,
  887.                  x, y, width, height, color, depth);
  888.       }
  889.  
  890.     /* Do the transfer one scan line at a time. */
  891.     { const byte *p = data;
  892.       int d = data_x;
  893.       int dstep = (step << 3) / depth;
  894.       int code = 0;
  895.       int i;
  896.  
  897.       for ( i = 0; i < height && code >= 0;
  898.             ++i, p += raster - step, d += dstep
  899.           )
  900.         code = (*copy_alpha)(dev, p, d, raster, gx_no_bitmap_id,
  901.                  x, y + i, width, 1, color, depth);
  902.       return code;
  903.     }
  904. }
  905.